
import { debounce } from "../../utils/util";
const nowTime = new Date().getTime();
const minTime = new Date(1900,1,1).getTime();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      value: "标题",
    },
    currentDate: {
      type: Date,
      value: nowTime,
    },
    maxDate: {
      type: Date,
      value: nowTime,
    },
    minDate: {
      type: Date,
      value: minTime,
    },
    type: {
      type: String,
      value: 'date',
    },

  },

  /**
   * 组件的初始数据
   */
  data: {

    show: false,
    showEl: false,
    // 时间label格式化显示
    dateLabelormatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      if (type === "day") {
        return `${value}日`;
      }
      return value;
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onShow() {
      this.setData({
        show: true,
        showEl: true,
      });
    },
    onClose() {
      const that = this;
      that.setData({
        show: false,
      });
      const debouncedHide = debounce(() => {
        that.setData({
          showEl: false,
        });
      }, 500);
      debouncedHide();
    },

    onInputBirthday(e) {
      console.log(e);
      this.setData({
        currentDate: e.detail,
      });
    },

    onConfirm(e) {
      const date = this.formatDate(e.detail);
      this.triggerEvent("onConfirm", date, e.detail);
      this.onClose();
    },

    // 通过时间戳格式化日期
    formatDate(timestamp) {
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");

      return `${year}-${month}-${day}`;
    },
  },
});
